package com.mw.controller;

import com.mw.domain.AjaxResult;
import com.mw.domain.Student;
import com.mw.domain.Classgroup;
import com.mw.domain.StudentAndClassgroup;
import com.mw.tool.BeanPropertyRowMapperWithPrefix;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.JdbcClient;
import org.springframework.web.bind.annotation.*;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@RestController
public class StudentController {
    @Autowired
    private JdbcClient jdbcClient;

    @GetMapping("/students")
    public AjaxResult getAll() {
        String sql = """
            select 
                s.id as s_id, s.name as s_name, s.code as s_code, s.password as s_password, s.remark as s_remark, s.class_id as s_classId,
                c.name as c_name, c.remark as c_remark, c.id as c_id
            from student s
            left join classgroup c
            on s.class_id = c.id
        """;

        // 定义 RowMappers，用于 Student 和 Classgroup 的映射
        BeanPropertyRowMapper<Student> rowMapper1 = new BeanPropertyRowMapperWithPrefix<>("s_");
        rowMapper1.setMappedClass(Student.class);
        BeanPropertyRowMapper<Classgroup> rowMapper2 = new BeanPropertyRowMapperWithPrefix<>("c_");
        rowMapper2.setMappedClass(Classgroup.class);

        // 自定义 RowMapper，返回 StudentAndClassgroup 对象
        RowMapper<StudentAndClassgroup> rowMapper = new RowMapper<StudentAndClassgroup>() {
            public StudentAndClassgroup mapRow(ResultSet rs, int rowNum) throws SQLException {
                // 映射 Student 和 Classgroup 对象
                Student student = rowMapper1.mapRow(rs, rowNum);
                Classgroup classgroup = rowMapper2.mapRow(rs, rowNum);

                // 构建 StudentAndClassgroup 对象
                StudentAndClassgroup studentAndClassgroup = new StudentAndClassgroup();
                studentAndClassgroup.setStudent(student);
                studentAndClassgroup.setClassgroup(classgroup);

                return studentAndClassgroup;
            }
        };

        List<StudentAndClassgroup> items = jdbcClient.sql(sql)
                .query(rowMapper)
                .list();

        return AjaxResult.success().selectResult(items.size(), items);
    }

    @GetMapping("/students/{id}")
    public AjaxResult getById(@PathVariable("id") int expectedId) {
        String sql = """
            select 
                s.id as s_id, s.name as s_name, s.code as s_code, s.password as s_password, s.remark as s_remark, s.class_id as s_classId,
                c.name as c_name, c.remark as c_remark, c.id as c_id
            from student s
            left join classgroup c
            on s.class_id = c.id
            where s.id = :id
        """;

        // 定义 RowMappers，用于 Student 和 Classgroup 的映射
        BeanPropertyRowMapper<Student> rowMapper1 = new BeanPropertyRowMapperWithPrefix<>("s_");
        rowMapper1.setMappedClass(Student.class);
        BeanPropertyRowMapper<Classgroup> rowMapper2 = new BeanPropertyRowMapperWithPrefix<>("c_");
        rowMapper2.setMappedClass(Classgroup.class);

        // 自定义 RowMapper，返回 StudentAndClassgroup 对象
        RowMapper<StudentAndClassgroup> rowMapper = new RowMapper<StudentAndClassgroup>() {
            public StudentAndClassgroup mapRow(ResultSet rs, int rowNum) throws SQLException {
                // 映射 Student 和 Classgroup 对象
                Student student = rowMapper1.mapRow(rs, rowNum);
                Classgroup classgroup = rowMapper2.mapRow(rs, rowNum);

                // 构建 StudentAndClassgroup 对象
                StudentAndClassgroup studentAndClassgroup = new StudentAndClassgroup();
                studentAndClassgroup.setStudent(student);
                studentAndClassgroup.setClassgroup(classgroup);

                return studentAndClassgroup;
            }
        };

        List<StudentAndClassgroup> items = jdbcClient.sql(sql)
                .param("id", expectedId)
                .query(rowMapper)
                .list();

        return AjaxResult.success().selectResult(items.size(), items);
    }

    @PutMapping("/students")
    public AjaxResult update(@RequestBody Student student) {
        String sql = """
            update student set 
                name = :name, 
                code = :code, 
                class_id = :classId, 
                remark = :remark 
            where id = :id
        """;

        int n = jdbcClient.sql(sql)
                .paramSource(student)
                .update();

        return AjaxResult.success().updateResult(n);
    }

    @DeleteMapping("/students/{id}")
    public AjaxResult deleteById(@PathVariable("id") int expectedId) {
        String sql = "delete from student where id = :id";

        int n = jdbcClient.sql(sql)
                .param("id", expectedId)
                .update();

        return AjaxResult.success().deleteResult(n);
    }

    @PostMapping("/students")
    public AjaxResult insert(@RequestBody Student student) {
        String sql = """
            insert into student
                (name, code, password, class_id, remark) 
            values
                (:name, :code, :password, :classId, :remark)
        """;

        int n = jdbcClient.sql(sql)
                .paramSource(student)
                .update();

        return AjaxResult.success().insertResult(n);
    }
}
